const app = getApp()
Page({
  data: {
    addressName: '佛山',
    addressDesc: '祖庙附近',
    where: '世贸天阶A1座',
    gdShangBanHour: "09", //规定上班时间-小时
    gdShangBanMinutes: "00", //规定上班时间-分钟
    gdXiaBanHour: "18", //规定下班时间-小时
    gdXiaBanMinutes: "00", //规定下班时间-分钟
    getHours: "00",
    getMinutes: "00",
    dataBoxM: null,
    dataBoxD: null,
    dayBox: null, //星期
    qdSJtimeHours: null, //实际上班打卡时间-小时
    qdSJtimeMinutes: null, //实际下班打卡时间-小时
    qtSJtimeHours: null, //实际上班打卡时间-小时
    qtSJtimeMinutes: null, //实际下班打卡时间-小时
    qdCDbox: 0, //迟到
    qtZTbox: 0, //早退
    qdBtn: 0, //签到按钮
    qtBtn: 0, //签退按钮
    reasonCD: null, //迟到理由
    reasonZT: null, //早退理由
    closeReason: 1,
    BSSID: null,//当前wifi信息
    gdBSSID:null,//合法的wifi信息
  },
  onLoad: function() {
    var that = this
    var timeClock=setInterval(function() {
      that.getTime()
    }, 500)
    var WiFiClock=setInterval(function(){
      that.getWiFiInfo()
    },2500)
  },
  //时钟
  getTime: function() {
    var that = this
    //获取当前时间
    var nowDate = new Date();
    var dayBox
    switch (nowDate.getDay()) {
      case 1:
        dayBox = "一";
        break;
      case 2:
        dayBox = "二";
        break;
      case 3:
        dayBox = "三";
        break;
      case 4:
        dayBox = "四";
        break;
      case 5:
        dayBox = "五";
        break;
      case 6:
        dayBox = "六";
        break;
      case 0:
        dayBox = "日";
        break;
    }
    // console.log(dayBox)
    that.setData({
      dataBoxM: nowDate.getMonth() + 1,
      dataBoxD: nowDate.getDate(),
      dayBox: dayBox,
    })
    that.setData({
      getHours: nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours(),
      getMinutes: nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes(),
    })
  },
  // 获取当前WiFi信息
  getWiFiInfo: function() {
    var that=this
    wx.startWifi({
      success: function() {
        wx.getConnectedWifi({
          success: function(res) {
            that.setData({
              BSSID: res.wifi.BSSID
            })
          }
        })
      }
    })
  },
  //签到方法
  qdBtn: function() {
    var thisTime = new Date()
    var that = this
    console.log(thisTime.getHours(), thisTime.getMinutes())
    that.setData({
      qdSJtimeHours: thisTime.getHours() < 10 ? "0" + thisTime.getHours() : thisTime.getHours(),
      qdSJtimeMinutes: thisTime.getMinutes() < 10 ? "0" + thisTime.getMinutes() : thisTime.getMinutes(),
      qdBtn: 1
    })
    if (that.data.gdShangBanMinutes != '00') {
      if (that.data.qdSJtimeHours < that.data.gdShangBanHour && that.data.qdSJtimeMinutes < that.data.gdShangBanMinutes) {
        //按时上班，没迟到
        console.log("打卡成功")
        wx.showToast({
          title: '打卡成功',
          icon: 'none'
        })
      } else {
        //描述迟到原因
        that.setData({
          qdCDbox: 1,
          closeReason: 2 //0显示早退，1隐藏，2显示迟到          
        })
      }
    } else {
      if (that.data.qdSJtimeHours < that.data.gdShangBanHour) {
        //按时上班，没迟到
        console.log("打卡成功")
        wx.showToast({
          title: '打卡成功',
          icon: 'none'
        })
      } else {
        //描述迟到原因
        that.setData({
          qdCDbox: 1,
          closeReason: 2 //0显示早退，1隐藏，2显示迟到          
        })
      }
    }

  },
  //签退方法
  qtBtn: function() {
    var thisTime = new Date()
    var that = this
    that.setData({
      qtSJtimeHours: thisTime.getHours() < 10 ? "0" + thisTime.getHours() : thisTime.getHours(),
      qtSJtimeMinutes: thisTime.getMinutes() < 10 ? "0" + thisTime.getMinutes() : thisTime.getMinutes(),
      qtBtn: 1
    })
    if (that.data.gdXiaBanMinutes != '00') {
      if (that.data.qtSJtimeHours >= that.data.gdXiaBanHour && that.data.qtSJtimeMinutes > that.data.gdXiaBanMinutes) {
        //按时下班，没早退
        console.log("打卡成功")
        wx.showToast({
          title: '打卡成功',
          icon: 'none'
        })
      } else {
        //描述早退原因
        that.setData({
          qtZTbox: 1,
          closeReason: 0 //0显示早退，1隐藏，2显示迟到            
        })
      }
    } else {
      if (that.data.qtSJtimeHours > that.data.gdXiaBanHour) {
        //按时下班，没早退
        console.log("打卡成功")
        wx.showToast({
          title: '打卡成功',
          icon: 'none'
        })
      } else {
        //描述早退原因
        that.setData({
          qtZTbox: 1,
          closeReason: 0 //0显示早退，1隐藏，2显示迟到            
        })
      }
    }
  },
  // 提交理由方法
  submit: function(e) {
    var that = this
    if(that.data.closeReason==2){
      console.log(e.detail.value.reason, "eee")
      if (e.detail.value.reason == '') {
        wx.showToast({
          title: '请输入理由',
          icon: 'none'
        })
      } else {
          that.setData({
            reasonCD: e.detail.value.reason
          })
        }
        that.setData({
          closeReason: 1 //0显示早退，1隐藏，2显示迟到  
        })
    }else{
      console.log(e.detail.value.reason, "eee")
      if (e.detail.value.reason == '') {
        wx.showToast({
          title: '请输入理由',
          icon: 'none'
        })
      } else {
          that.setData({
            reasonZT: e.detail.value.reason
          })
        that.setData({
          closeReason: 1 //0显示早退，1隐藏，2显示迟到  
        })
      }
    }

  }

})